﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Models;
using Common;
using DbUtility;

namespace Web.Areas.Admin.Controllers
{
    public class LotteryController : BaseController
    {
        public ActionResult Index(LotterySearch search)
        {
            lotterylogLogic ll = new lotterylogLogic();
            var list = ll.Getlotterylogs(new lotterylog(), PageIndex, PageSize, search.GenerateWhere(), "createdate", "desc");
            if (Request.IsAjaxRequest())
            {
                return PartialView("_Index", list);
            }
            return View(list);
        }
        [HttpGet]
        public ActionResult Export()
        {
            try
            {
                DBContext db = new DBContext();
                var dataTable = db.ExecuteDataTableSql("select * from lotterylog where isdeleted=@isdeleted order by createdate", new List<System.Data.Common.DbParameter>()
                {
                    new System.Data.SqlClient.SqlParameter("@isdeleted",false){SqlDbType=System.Data.SqlDbType.Bit},
                });
                var dic = new Dictionary<string, string>
                {
                    {"id", "中奖编号"},
                    {"createdate", "中奖时间"},
                    {"nickname", "用户昵称"},
                };
                ExcelHelper.DataTableToExcel(dataTable, dic, "中奖名单列表" + DateTime.Now.Format("yyyyMMddHHmmss"));
                return Content("");
            }
            catch (Exception ex)
            {
                return ErrorMsg(ex.Message);
            }
        }

        [HttpPost]
        public ActionResult ClearData()
        {
            try
            {
                DBContext db = new DBContext();
                var result = db.ExecuteNonQuerySql("update lotterylog set isdeleted = 1 where isdeleted=0");
                return SuccessMsg("清除成功！");
            }
            catch (Exception e)
            {
                return ErrorMsg("清除失败，网络错误！");
            }
        }
    }
}
